using UnityEngine;
using System.Collections;

public class Cam : MonoBehaviour {

	public Transform target;
	public Transform cursor;
	

	// Use this for initialization
	void Start () {

	}
	
	// Update is called once per frame
	void Update () {
	
		if(target == null || cursor == null)
			return;
	
		
		if(Time.timeSinceLevelLoad < 0.05f)
		{
			transform.position = new Vector3(target.position.x,target.position.y,
		                                 transform.position.z);
		}
		
		
		
		transform.Translate(-(transform.position.x-(target.position.x+cursor.position.x)/2f)*Time.deltaTime, 
		                    -(transform.position.y-(target.position.y+cursor.position.y)/2f)*Time.deltaTime,
							0);
		float dist = Vector3.Distance(new Vector2(transform.position.x,transform.position.y),
		                              new Vector2(target.position.x,target.position.y));
		if(dist > 4f)
		{
			Vector3 dir = target.position - transform.position;
			dir.z = 0f;
			dir = dir.normalized * (dist - 4f);
			
			transform.Translate(dir);
		}
		

	}
}
